<?php
/*
Plugin Name: JGL Javascript Slideshow
Plugin URI: http://www.joelgethinlewis.com
Description: A plugin allows you to add javascript slideshows to posts. Thanks to <a href="http://www.mudcorporation.com/">Takashi Okamoto</a> and <a href="http://www.martinet.nl/">Kamiel Martinet</a>
Author: Joel Gethin Lewis
Version: 1.0
Author URI: http://www.joelgethinlewis.com

TODO: add another filter that cuts out the plugin when it is in summary mode, or the one that is in the index view (i.e. not the single page)

*/

global $jgl_slideshow_counterOfSlideshows;

$jgl_slideshow_counterOfSlideshows=0;

function jgl_slideshow_the_content($content)
{ 
  global $jgl_slideshow_counterOfSlideshows;
  
  $inSlideshow    = false;
  $slideshowStart = false;
  $firstImage = true;
  $images       = "";
  $output       = "";
  $firstImageURL = "";
  $imageIndex = 0;
  $firstImageTitle = "";
  $firstImageCaption = "";

  $lines = split("\n",stripslashes($content));
  
  //echo "The count of lines by JGL is ".count($lines);
  
  foreach($lines as $theLine)
  {

    // check if start of codeblock
    if(preg_match("/\[slideshow([^\]]*)\]/",$theLine))
	{
      // fix me
      $theLine = str_replace('&#8221;','"',$theLine);
      $theLine = str_replace('&#8243;','"',$theLine);
      // Parse optional attributes in arguments array
      preg_match_all('/(\w*)="(.*?)"/i',$theLine,$attributes);
      $arguments = array();
      foreach($attributes[1] as $key=>$value)
	  {
        $arguments[$value] = $attributes[2][$key];
      }
      $inSlideshow = true;
      $slideshowStart = true;
      $theLine="";
    }

	
    // Check if end of codeblock
    if(preg_match("/\[\/slideshow\]/",$theLine) && $inSlideshow)
	{
      $inSlideshow = false;

	  //get the script tags together first
	  
	  $output .= "<script type='text/javascript'>\n";
	  $output .= "var imgs".$jgl_slideshow_counterOfSlideshows."Slideshow = new Array();\n";
	  $output .= "var imgs".$jgl_slideshow_counterOfSlideshows.";\n";
	  $output .= $images;
	  $output .= "imgs".$jgl_slideshow_counterOfSlideshows." = new MudFadeGallery('imgs".$jgl_slideshow_counterOfSlideshows."', 'imgDisplay".$jgl_slideshow_counterOfSlideshows."', imgs".$jgl_slideshow_counterOfSlideshows."Slideshow, {startNum: 0, preload: true, autoplay: 0});\n";
	  $output .= "</script>\n";
	  
	  //now the html
	  
	  $output .= "<div class='jglSlideshow'>\n";  
	  $output .= "<div class='imgs-frame'>\n";
	  $output .= "<a href='#' onclick='imgs".$jgl_slideshow_counterOfSlideshows.".nextImg(); return false;'>\n";
	  $output .= "<img id='imgDisplay".$jgl_slideshow_counterOfSlideshows."' src='".$firstImageURL."' alt='Slideshow image' width='900'  height='600' />\n";
	  $output .= "</a>\n";
	  $output .= "</div>\n";
	  $output .= "<div class='controller'>\n";
	  $output .= "<ul>\n";
	  $output .= "<li>\n";
	  $output .= "Slideshow: <a href='#' onclick='imgs".$jgl_slideshow_counterOfSlideshows.".prevImg(); return false;'>&lt;</a> <a href='#' onclick='imgs".$jgl_slideshow_counterOfSlideshows.".nextImg(); return false;'>&gt;</a> | \n";
	  //loop to spit out $imageIndex bits
	  for ($i = 0; $i < $imageIndex; $i++) 
	  {
		$output .= '<a href="#" onclick="imgs'.$jgl_slideshow_counterOfSlideshows.'.showImg('.$i.'); return false;">';
		$output .= (1+$i)."</a>\n";
      }	  
	  $output .= "| <a href='#' onclick='imgs".$jgl_slideshow_counterOfSlideshows.".apStart(7); return false;'>Start</a> / <a href='#' onclick='imgs".$jgl_slideshow_counterOfSlideshows.".apStop(); return false;'>Stop</a>\n";	  
	  $output .= "</li>\n";
	  $output .= "<li id='imgDisplay".$jgl_slideshow_counterOfSlideshows."_number'> 1 of ".$imageIndex." images</li>\n";
	  $output .= "<li id='imgDisplay".$jgl_slideshow_counterOfSlideshows."_title'><br/>".$firstImageTitle.". ".$firstImageCaption."</li>\n";	  
	  $output .= "</ul></div></div>\n";
	  
	  //increment the slideshow counter
	  $jgl_slideshow_counterOfSlideshows++; 
      
	  $images="";
      $theLine="";
	  
	  unset($arguments);
      unset($attributes);
	  
	  $firstImage = true;
	  $firstImageURL = "";
	  
	  $imageIndex = 0;
    }

    if($inSlideshow)
	{
      
	  if($slideshowStart) 
	  {
        $slideshowStart = false;
      } 
	  else 
	  {
	  	if($firstImage)
		{
			$firstImageURL = jgl_slideshow_get_first_image_url($theLine, $arguments);
			$firstImageTitle = jgl_slideshow_get_first_image_title($theLine, $arguments);
			$firstImageCaption = jgl_slideshow_get_first_image_caption($theLine, $arguments);
			$firstImage = false;
		}
        $images .= jgl_slideshow_format_image($theLine,$arguments, $imageIndex, $jgl_slideshow_counterOfSlideshows);
		
		$imageIndex++;	
      }
    } 
	else 
	{
      // do nothing (just append line to output string)
      $output .= $theLine."\n";
    }
  }
  
  return $output;	
}

function jgl_slideshow_get_first_image_url($string, $options)
{
  $string = str_replace('<br />','',$string);
  
  if (!strpos($string,':')===false) 
  {
    $imageArray = split(':',$string);
    
	//allowing for basepath option
	if (array_key_exists('basepath',$options) && (strpos($imageArray[0],'/')===false))
	{
		$path = $options['basepath'].$imageArray[0];
	}
	else
	{
		$path = $imageArray[0];
    }
    return $path;
  } 
  else 
  {
  	//try and get anything out, doesn't seem to be following the correct syntax
    //return '<img src="'.$string.'" alt="" />';
	return "Error in first image url syntax.";
  }
}

function jgl_slideshow_get_first_image_title($string, $options)
{
  $string = str_replace('<br />','',$string);
  
  if (!strpos($string,':')===false) 
  {
    $imageArray = split(':',$string);
	
    return $imageArray[1];
  } 
  else 
  {
  	//try and get anything out, doesn't seem to be following the correct syntax
    //return '<img src="'.$string.'" alt="" />';
	return "Error in first image title syntax.";
  }
}

function jgl_slideshow_get_first_image_caption($string, $options)
{
  $string = str_replace('<br />','',$string);
  
  if (!strpos($string,':')===false) 
  {
    $imageArray = split(':',$string);
	
    return $imageArray[2];
  } 
  else 
  {
  	//try and get anything out, doesn't seem to be following the correct syntax
    //return '<img src="'.$string.'" alt="" />';
	return "Error in first image caption syntax.";
  }
}

function  jgl_slideshow_format_image($string, $options, $imgIndex, $slideshowIndex)
{
  $string = str_replace('<br />','',$string);
  
  if (!strpos($string,':')===false) 
  {
    $imageArray = split(':',$string);
    
	//allowing for basepath option
	if (array_key_exists('basepath',$options) && (strpos($imageArray[0],'/')===false))
	{
		$path = $options['basepath'].$imageArray[0];
	}
	else
	{
		$path = $imageArray[0];
    }
	
    //$image = '<img src="'.$path.'" alt="'.$imageArray[1].'/>';
	
	//$image = "The image source is ".$path." its caption is ".$imageArray[1].".";
	
	$galleryJavascriptReference = "imgs".$slideshowIndex."Slideshow[".$imgIndex."]";
	
	$image .= $galleryJavascriptReference." = new Object();\n";
	$image .= $galleryJavascriptReference.".image = \"".$path."\";\n";
	$image .= $galleryJavascriptReference.".title = \"".$imageArray[1]."\";\n";
	$image .= $galleryJavascriptReference.".caption = \"".$imageArray[2]."\";\n";
	
    return $image;
  } 
  else 
  {
  	//try and get anything out, doesn't seem to be following the correct syntax
    //return '<img src="'.$string.'" alt="" />';
	return "Error in slideshow syntax.";
  }
}

add_filter('the_content', 'jgl_slideshow_the_content');

function jgl_slideshow_wp_head($unused)
{
	//insert the necessary javascript links to the head section of every page

	echo "<script src='".get_settings('siteurl')."/wp-content/plugins/jgl_slideshow/_lib/js/prototype.js' type='text/javascript'></script>\n";
 	echo "<script src='".get_settings('siteurl')."/wp-content/plugins/jgl_slideshow/_lib/js/mud_FadeGallery.js' type='text/javascript'></script>\n";
	
	//and now the stylesheet
	
	echo "<link rel='stylesheet' type='text/css' media='screen' href='".get_settings('siteurl')."/wp-content/plugins/jgl_slideshow/_lib/css/jglSlideshow.css'/>\n";
	//and now check for compatibility
	
	echo "<script src='".get_settings('siteurl')."/wp-content/plugins/jgl_slideshow/_lib/js/browserCheck.js' type='text/javascript'></script>\n";
}

add_action('wp_head', 'jgl_slideshow_wp_head');
?>
